﻿#region Copyright

// All Material Copyright © 2010 Dynamic Concepts Development Corp., All Rights Reserved.
//      Portions Copyright © 1984-2009 Dynamic Concepts Development Corp., All Rights Reserved.
// For further information please contact: SoftwareSupport@dynconcepts.com
// Dynamic Concepts Development Corp.  311 W 57th St. Suite #422, New York, NY 10019

#endregion

#region Using References

using System;
using System.Threading;
using System.Windows.Threading;
using DynConDevCorp.OpenArch.SupportLibrary.ApplicationInfrastructure;
using DynConDevCorp.OpenArch.SupportLibrary.ParallelExecution.Events;

#endregion

namespace DynConDevCorp.OpenArch.DynAppFramework.Tiers.UserInterface.WPF.Engine
{
    public static class UIExecutionEnvironment
    {
        #region Public Methods

        public static void Execute(Object originator, UIEnvironmentDelegate action, object context, ParallelEventDispatchMode eventDispatchMode)
        {
            UIExecutionEnvironmentEvent.DispatchMode = eventDispatchMode;
            UIExecutionEnvironmentEvent.Execute(originator, new UIExecutionEnvironmentEventArgs(action, context), EventCompleted);
        }

        public static void Initialize()
        {
            s_Dispatcher = Dispatcher.CurrentDispatcher;
            s_ExecutionThread = Thread.CurrentThread;
            UIExecutionEnvironmentEvent.Add(new ParallelEventHandler<UIExecutionEnvironmentEventArgs>(EventTarget), "Task");
        }

        #endregion

        #region Private Methods

        private static void EventCompleted(object sender, EventArgs args)
        {
            QueuedLogger.GlobalInstance.Log(QueuedLogger.MessageSeverity.Debug, "EventCompleted()");
        }

        private static void EventTarget(object sender, UIExecutionEnvironmentEventArgs args)
        {
            QueuedLogger.GlobalInstance.Log(QueuedLogger.MessageSeverity.Debug, "EventTarget() - Executed!!!!");
            if (args != null)
            {
                args.ExecuteAction();
            }
        }

        #endregion

        #region Static Fields and Constants

        internal static readonly ParallelEvent<UIExecutionEnvironmentEventArgs> UIExecutionEnvironmentEvent = new ParallelEvent<UIExecutionEnvironmentEventArgs>();
        private static Dispatcher s_Dispatcher;
        private static Thread s_ExecutionThread;

        #endregion
    }

    public delegate void UIEnvironmentDelegate(object context);

    internal class UIExecutionEnvironmentEventArgs : EventArgs
    {
        #region Constructors

        internal UIExecutionEnvironmentEventArgs(UIEnvironmentDelegate action, object context)
        {
            r_Action = action;
            r_Context = context;
        }

        #endregion

        #region Internal Properties and Indexers

        internal Delegate Action
        {
            get { return r_Action; }
        }

        internal object Context
        {
            get { return r_Context; }
        }

        #endregion

        #region Internal Methods

        internal void ExecuteAction()
        {
            if (r_Action != null)
            {
                r_Action(r_Context);
            }
        }

        #endregion

        #region Instance Fields (ReadOnly)

        private readonly UIEnvironmentDelegate r_Action;
        private readonly object r_Context;

        #endregion
    }
}